//全局变量
var methodDbName = 'changeCurDb';
var methodProName = 'changeCurPro';
var methodBatchName = 'initTopBatch';
var idCurDb = $("#curDbCode");
var idCurPro = $("#curProCode");
var topsData = $('#tops');
var thisUser = '';
var btachType = '';
$(function(){ 
	loadUser(); 
});

//加载用户登陆信息
function loadUser(){
	/*var _url = "stm/stmUser/stmUser_proFile.action";
	
	$.ajax({
		type : "POST",
		dataType : "json",
		url : _url,
		success : function(data) {
			if(data.user){
				$(".user-info").append("<span class ='userName'>"+data.user.fdName+"</span>");
				$("#Personal").attr("href","javascript:loadUserData("+ data.user.fdKey +")");
				$("#pwd").attr("href","javascript:showUserModal("+ data.user.fdKey +")");
				$("#prvUser").attr("href","javascript:showUserPrvLg("+ data.user.fdKey +")");
				findCurProject(data.user.fdKey);
				initTopBatch();
				thisUser = data.user.fdName;
			}
		}
	})*/
}

/**
 * 加载jqgrid数据
 * @param _url 地址
 * @param _id 	控件ID
 */
function loadJqGrid(_url,_id){

	$("#"+_id).jqGrid('setGridParam',{  
		url:_url, 
		datatype:"json" 
	}).trigger("reloadGrid");
}

//跳转页面到用户查看页
function loadUserData(fdKey){
	$('#loadUser').val(1);
	if(thisUser == 'admin'){
		$('#isAdmin').val(0);
	}else{
		$('#isAdmin').val(1);
	}
	$("#content").load("jsp/stm/stmUser/stm_user_edit.jsp",function(){
		loadJqGrid("stm/stmUserProject/stmUserProject_list.action?stmUserKey.fdKey="+fdKey,"grid-table-user");
		var brd = $("#menuList");
		var lable = "<li><a href='indexace.jsp'><i class='ace-icon fa fa-home home-icon'></i>JDCS</a></li>";
		lable += "<li><i class=''></i>"+"系统管理"+"</li>";
		lable +="<li><i class=''></i>"+"用户管理"+"</li>";
		brd.empty();
		brd.append(lable);
		loadData(fdKey);
		editData(false);
		$("#editButton").attr("disabled","disabled");
		$("#initButton").hide();
		$("#returnButton").hide();
		$("#deleteButton").attr("disabled","disabled");
		
		$.ajax({
			type:'POST',
			dataType:'JSON',
			url : "stm/stmUser/stmUser_init.action",
			success:function(data){
				if(data.success == true){
					$("#initButton").attr("disabled","disabled");
				}else{
					$("#initButton").hide();
					$("#add_grid").attr("disabled","disabled");
					$("#delete_grid").attr("disabled","disabled");
				}
			}	
		})
	});
}
//获取当前项目
function findCurProject(data){
	var  arrtUl= document.getElementById("curProCode");
	var _url = "stm/stmUserProject/stmUserProject_findCurProCode.action?stmUserKey.fdKey="+data;
	var _urlLi = "stm/stmUserProject/stmUserProject_findLiCurProCode.action?stmUserKey.fdKey="+data;
	$.ajax({
		url:_url,
		type:'POST',
		success:function(data){
			if(data.success){
				findCurDb(data.model.stmProjectKey.fdKey);
				liCurDb(_urlLi,methodProName,idCurPro,data.model.stmProjectKey.fdKey,null);
			}else{
				findCurDb(null);
				liCurDb(_urlLi,methodProName,idCurPro,null,null);
			}
		},
		error:function(data){
			$.gritter.add({
				title: '<center>操作提示</center>',
				text: "<center><strong><i class='ace-icon fa fa-times'></i> "+"抓取当前项目操作失败"+"!</center>",
				time:2000,
				class_name: 'gritter-error gritter-center gritter-light'
			});
		},
		dataType : 'json'
	});
}
//获取当前数据源
function findCurDb(fdKey){ 
	var  arrtUl= document.getElementById("curDbCode");
	var _urlLi = "stm/stmDataBase/stmDataBase_findLiCurDbCode.action?projectKey.fdKey="+fdKey;
	liCurDb(_urlLi,methodDbName,idCurDb,null,getDataSource());
}
//切换当前项目
function changeCurPro(fdKey,userKey){
	var menuI = $("#"+fdKey).find("i");
	if(menuI.length > 0){
		$.gritter.add({
			title: '<center>操作提示</center>',
			text: "<center><strong><i class='ace-icon fa fa-exclamation-triangle'></i> "+"该项目已经是当前项目，不需要在进行切换"+"!</center>",
			time:2000,
			class_name: 'gritter-warning gritter-center gritter-light'
		});
		return;
	}
	var _urlLi = "stm/stmUserProject/stmUserProject_findLiCurProCode.action?stmUserKey.fdKey="+userKey;
	var _url = "stm/stmUserProject/stmUserProject_changeProCode.action?stmProjectKey.fdKey="+fdKey+"&stmUserKey.fdKey="+userKey;
	var  arrtUl= document.getElementById("curProCode");
	liCurDb(_urlLi,methodProName,idCurPro,fdKey,null);
	$.ajax({
		type:"POST",
		dataType:"json",
		url:_url,
		success : function(data) {
			if (data.success != -1) {
				$.gritter.add({
					title: '<center>操作提示</center>',
					text: "<center><strong><i class='ace-icon fa fa-check'></i> "+"切换项目成功"+"!</center>",
					time:2000,
					class_name: 'gritter-success gritter-center gritter-light'
				}); 
				findCurProject(userKey);
				findCurDb(data.model.stmProjectKey.fdKey);
				var menuA = $(".open").children().find("li.active").find("a").first();
				var menuUrl = menuA.attr('rel');
				if(menuUrl!=undefined||menuUrl==""){
					$('#content').load(menuUrl);
				}
				setTimeout(function(){
					window.location.reload();
				},800);
				
			} else {
				$.gritter.add({
					title: '<center>操作提示</center>',
					text: "<center><strong><i class='ace-icon fa fa-times'></i> "+"切换项目错误：" + data.message+"!</center>",
					time:2000,
					class_name: 'gritter-error gritter-center gritter-light'
				});
				findCurProject(userKey);
				findCurDb(data.model.stmProjectKey.fdKey);
				var menuA = $(".open").children().find("li.active").find("a").first();
				var menuUrl = menuA.attr('rel');
				if(menuUrl!=undefined||menuUrl==""){
					$('#content').load(menuUrl);
				}
				setTimeout(function(){
					window.location.reload();
				},800);
				return;
			}
		},
		error :function(data){
			$.gritter.add({
				title: '<center>操作提示</center>',
				text: "<center><strong><i class='ace-icon fa fa-times'></i> "+"切换项目错误：" + data.message+"!</center>",
				time:2000,
				class_name: 'gritter-error gritter-center gritter-light'
			});
			return;
		}
	});
}

//切换数据源
function changeCurDb(fdKey,fdCode){
//	dbischecked(fdCode);
	
	var i = $("#curDbCode").find("i");
	var li = $(i).parent().parent();
	if($(li).hasClass(fdCode)) {
		$.gritter.add({
			title: '<center>操作提示</center>',
			text: "<center><strong><i class='ace-icon fa fa-exclamation-triangle'></i> "+"该数据源已经是当前数据源，不需要在进行切换"+"!</center>",
			time:2000,
			class_name: 'gritter-warning gritter-center gritter-light'
		});
		return;
	}
	
	var _urlLi = "stm/stmDataBase/stmDataBase_findLiCurDbCode.action?projectKey.fdKey="+fdKey;
	var _url = "pns/gvSession/gvSession_changeDbCode.action?dbCode="+fdCode;
	//var  arrtUl= document.getElementById("curDbCode");
	$.ajax({
		type:"POST",
		dataType:"json",
		url:_url,
		success : function(data) { 
			if (data.success) {
				$.gritter.add({
					title: '<center>操作提示</center>',
					text: "<center><strong><i class='ace-icon fa fa-check'></i> "+"切换数据源成功"+"!</center>",
					time:2000,
					class_name: 'gritter-success gritter-center gritter-light'
				});
				liCurDb(_urlLi,methodDbName,idCurDb,fdKey,fdCode);
				var menuA = $(".open").children().find("li.active").find("a").first();
				var menuUrl = menuA.attr('rel');
				if(menuUrl!=undefined||menuUrl==""){
					$('#content').load(menuUrl);
				}
			} else {
				$.gritter.add({
					title: '<center>操作提示</center>',
					text: "<center><strong><i class='ace-icon fa fa-times'></i> "+"切换数据源错误：" + data.message+"!</center>",
					time:2000,
					class_name: 'gritter-error gritter-center gritter-light'
				});
			}
		},
		error :function(data){
			$.gritter.add({
				title: '<center>操作提示</center>',
				text: "<center><strong><i class='ace-icon fa fa-times'></i> "+"切换数据源错误：" + data.message+"!</center>",
				time:2000,
				class_name: 'gritter-error gritter-center gritter-light'
			});
		}
	});
}

//初始化li标签
function liCurDb(_url,methodName,id,fdKey,fdCode){
	var initLi = "";
	$.ajax({
		async:false,
		url:_url,
		type:'POST',
		success:function(data){
			$(id).empty();
			var _fdKey;
			var _userKey;
			if(data != null){
				for(var i = 0; i < data.length;i++){
					if(methodName == "changeCurDb"){
						if(data[i].model.fdCode==fdCode){
							initLi +="<li class="+data[i].model.fdCode+"><a href='javascript:"+methodName+"("+data[i].model.projectKey.fdKey+",\""+data[i].model.fdCode+"\")' id='"+ data[i].model.projectKey.fdKey +"'>"+"<i class='green fa fa-check'></i>&nbsp;&nbsp;&nbsp;&nbsp;"+ data[i].model.fdName+"</a></li>";
						}else{
							initLi +="<li class="+data[i].model.fdCode+"><a href='javascript:"+methodName+"("+data[i].model.projectKey.fdKey+",\""+data[i].model.fdCode+"\")' id='"+ data[i].model.projectKey.fdKey +"'>"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ data[i].model.fdName+"</a></li>";	
						} 
					}else{
						_fdKey = data[i].model.stmProjectKey.fdKey;
						_userKey = data[i].model.stmUserKey.fdKey;
						if(_fdKey==fdKey){
							initLi +="<li><a href='javascript:"+methodName+"("+_fdKey+","+_userKey+")' id='"+ _fdKey +"'>"+"<i class='green fa fa-check'></i>&nbsp;&nbsp;&nbsp;&nbsp;"+ data[i].model.stmProjectKey.fdName+"</a></li>";
						}else{
							initLi +="<li><a href='javascript:"+methodName+"("+_fdKey+","+_userKey+")' id='"+ _fdKey +"'>"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ data[i].model.stmProjectKey.fdName+"</a></li>";
						}
					}
				}
				$(id).append(initLi);
			}
			if(data == null || data.length < 1 ){
				if(methodName == "changeCurDb"){
					$(id).append("<li><a href='#'>"+"当前项目下没有对应数据源"+"</a></li>");
				}else{
					$(id).append("<li><a href='#'>"+"当前环境下没有对应项目"+"</a></li>");
				}
			}	
		},
		error:function(data){
			$.gritter.add({
				title: '<center>操作提示</center>',
				text: "<center><strong><i class='ace-icon fa fa-times'></i> "+"操作失败"+"!</center>",
				time:2000,
				class_name: 'gritter-error gritter-center gritter-light'
			});
		},
		dataType : 'json'
	});
}

function initTopBatch(){
	//我的批次
	$.ajax({
		url : "tr/trBatchHd/trBatchHd_selectTop.action",
		type : "POST",
		dataType : "JSON",
		success : function(data){
			var initLi = "";
			for(var i = 0; i < data.length;i++){
				initLi +="<li><a value = '"+ data[i].fdKey +"' style = 'border-top: 0px' href = 'javaScript:loadBatchs("+ data[i].fdKey +","+ 1 +");'><span>"+data[i].fdCode+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ data[i].nodeCurrent.dictNodeType.fdCode +"</span></a></i></li>";
				btachType = data[i].batchType.fdName;
			}
			$("#tops").append(initLi+
					"<li><a href = 'javaScript:loadBatchAll(" + 1 + ");'>更多我的批次</a></li>");
		}
	});
	//待审批次
	$.ajax({
		url : "tr/trBatchHd/trBatchHd_selectPenTop.action",
		type : "POST",
		dataType : "JSON",
		success : function(data){
			var initLi = "";
			for(var i = 0; i < data.length;i++){
//				
				initLi +="<li><a value = '"+ data[i].fdKey +"' style = 'border-top: 0px' href = 'javaScript:loadBatchs("+ data[i].fdKey +","+ 2 +");'><span>"+data[i].fdCode+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ data[i].nodeCurrent.dictNodeType.fdCode +"</span></a></i></li>";
				btachType = data[i].batchType.fdName;
			}
			$("#penTops").append(initLi+
					"<li><a href = 'javaScript:loadBatchAll(" + 2 + ");'>更多待审批次</a></li>");
		}
	});
}

function loadBatchs(myKeys,type){
	var viewKey = myKeys;
	var crtUsr = thisUser;
	var _limit = valLimit(viewKey);
	
	if(type == 1){
		$('.grey ul li').find("i").remove();
		$('.purple ul li').find("i").remove();
		var var_b = $('.grey ul li').find("a[value="+viewKey+"]");
		$(var_b).find("span").before("<i class='green fa fa-check'></i>");
	}
	if(type == 2){
		$('.grey ul li').find("i").remove();
		$('.purple ul li').find("i").remove();
		var var_a = $('.purple ul li').find("a[value="+viewKey+"]");
		$(var_a).find("span").before("<i class='green fa fa-check'></i>");
	}
	
	$('#content').load('jsp/tr/trBatchHd/tr_batch_hd_edit.jsp?fdKey='+viewKey,function(){
		var brd = $("#menuList");
		var lable = "<li><a href='indexace.jsp'><i class='ace-icon fa fa-home home-icon'></i>JDCS</a></li>";
		lable +="<li><i class=''></i>传输请求</li>"+"<li><i class=''></i>批次管理</li>";
		brd.empty();
		brd.append(lable);
		
		loadData(viewKey);
		//加载批次对象列表页面
		if (crtUsr != loginUsr) {
			refreshTab(viewKey,_limit,false);
		} else {
			refreshTab(viewKey,_limit,true);
		}
		editBatchData(false,_limit,btachType);
	});
}

function loadBatchAll(actionType){
	$('#batchUserKey').val(actionType);
	$('.grey ul li').find("i").remove();
	$('.purple ul li').find("i").remove();
	$('#content').load('jsp/tr/trBatchHd/tr_batch_hd_list.jsp',function(){
		var brd = $("#menuList");
		var lable = "<li><a href='indexace.jsp'><i class='ace-icon fa fa-home home-icon'></i>JDCS</a></li>";
		lable +="<li><i class=''></i>传输请求</li>"+"<li><i class=''></i>批次管理</li>";
		brd.empty();
		brd.append(lable);
	});
}

/**
 * 刷新页面数据,加载
 */
function refreshTab(_fdKey,_limit,flag){
	if(_fdKey = -1){
		_fdKey = $("#fdKey").val();
	}
	//加载批次对象列表页面
	$("#object").load('jsp/tr/trBatchObject/tr_batch_object_list.jsp',function(){
		loadData(_fdKey,_limit);
	});
	//判断是否是数据库批次类型
	var proName= $('#_batchType').find("option:selected").text();
	if(proName =="JAVA项目"){
		$("#scp").hide();
		return;
	}

	//加载脚本列表页面
	$("#script").load('jsp/tr/trBatchScript/tr_batch_script_list.jsp',function(){
		loadData(_fdKey,_limit);
	});
	//加载大批次列表页面
	$("#bigBatch").load('jsp/tr/trBigBatch/tr_bigBatch_list.jsp',function(){
		loadNewData(flag);
	});
}

/**
 * 加载权限相关的限制
 * return readonly :只能查看
 * 
 */
function valLimit(viewKey){
	var _url = "tr/trBatchHd/trBatchHd_checkOper.action?fdKey="+viewKey;
	var result;
	$.ajax({
		type: 'POST',
		async: false,
		url: _url,
		success: function(data){
			if(data!=null){
				result= data.success;
			}
		},
		error: function(data) {

			return -1;
		},
		dataType: 'json'
	});
	return result;
}